if(!Pb) var Pb={};
//空间坐标点
Pb.Dot=function (pos){
		this.x=pos.x;
		this.y=pos.y;
		this.z=pos.z;
	}
Pb.Image = (function () {
    var sCss = document.createElement('canvas'),
        sCtt = sCss.getContext('2d'),
		dotSpace=14,
        fontSize = 500,
        fontFamily = 'Avenir, Helvetica Neue, Helvetica, Arial, microsoft yahei, sans-serif';

    function resetCanvasSize() {
        sCss.width = Math.floor(window.innerWidth / dotSpace) * dotSpace;
        sCss.height = Math.floor(window.innerHeight / dotSpace) * dotSpace;
        sCtt.fillStyle = 'red';
        sCtt.textBaseline = 'middle';
        sCtt.textAlign = 'center';
    }
    //返回图形中的所有点，以及点的最大高度和宽度
    function getImagePoints() {
        var pixels = sCtt.getImageData(0, 0, sCss.width, sCss.height).data,
            dots = [],
            x = 0,
            y = 0,
            fx = sCss.width,
            fy = sCss.height,
            w = 0,
            h = 0;

        for (var p = 0; p < pixels.length; p += (4 * dotSpace)) {
            if (pixels[p + 3] > 0) {
                dots.push(new Pb.Dot({
                    x: x,
                    y: y
                }));

                w = x > w ? x : w;
                h = y > h ? y : h;
                fx = x < fx ? x : fx;
                fy = y < fy ? y : fy;
            }

            x += dotSpace;

            if (x >= sCss.width) {
                x = 0;
                y += dotSpace;
                p += dotSpace * 4 * sCss.width;
            }
        }
        return { dots: dots, w: w + fx, h: h + fy };
    }

    function setContextFontSize(s) {
        sCtt.font = 6 * hFont * s + 'px ' + fontFamily;
    }

    return {
        drawImageFile: function (url, space, callback) {
			dotSpace=space||14;
			resetCanvasSize()
            var image = new Image();

            image.onload = function () {
                sCtt.clearRect(0, 0, sCss.width, sCss.height);
				if(1.0*this.width/this.height>1.0*window.innerWidth/window.innerHeight)
					sCtt.drawImage(this, 0, 0, window.innerWidth, 1.0 * window.innerWidth*this.height/this.width);
				else
					sCtt.drawImage(this, 0, 0, 1.0*window.innerHeight*this.width/this.height, window.innerHeight);
                callback(getImagePoints());
            };

            image.onerror = function () {
                callback(Pb.Image.drawLetter('welcome'));
            };

            image.src = url;
        },

        drawLetter: function (l) {
            var s = 1;

            setContextFontSize(1);

            sCtt.clearRect(0, 0, sCss.width, sCss.height);
            if (l.length >= 4) {
                setContextFontSize(0.5);
            } else if (l.length == 3) {
                setContextFontSize(0.75);
            }

            if (l.toString().indexOf('/') != -1) {

                sCtt.fillText(l.substr(0, l.toString().indexOf('/')), sCss.width / 2, sCss.height * 0.15);
                sCtt.fillText(l.substr(l.toString().indexOf('/') + 1), sCss.width / 2, sCss.height * 0.6);
            } else {

                sCtt.fillText(l, sCss.width / 2, sCss.height / 2);
            }

            return getImagePoints();
        },

    };
}());